package com.sgb.knowledgeshare.view.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.weight.VideoSliceSeekBar;
import com.swgk.core.util.ScreenUtil;
import java.lang.ref.SoftReference;
import java.util.ArrayList;

/**
 * Created by：冯齐 on 2020/11/24 15:04
 * Description：
 */
public class VideoTrimAdapter  extends BaseAdapter {
    private Context mContext;
    private int screenWidth;
    private ArrayList<SoftReference<Bitmap>> mBitmaps;
    private VideoSliceSeekBar seekBar;

    public VideoTrimAdapter(Context context, ArrayList<SoftReference<Bitmap>> bitmaps, VideoSliceSeekBar seekBar) {

        mContext = context;

        this.mBitmaps = bitmaps;

        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);

        assert wm != null;
        screenWidth = wm.getDefaultDisplay().getWidth();
        this.seekBar = seekBar;
    }

    /**
     * 添加一个元素
     */
    public void add(SoftReference<Bitmap> data) {
        if (mBitmaps == null) {
            mBitmaps = new ArrayList<>();
        }
        mBitmaps.add(data);
        notifyDataSetChanged();


    }

    private int getItemCount() {

        return mBitmaps == null ? 0 : mBitmaps.size();
    }


    @Override
    public int getCount() {
        return getItemCount();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();

            convertView = LayoutInflater.from(mContext).inflate(R.layout.crop_item_trim_video_thumbnail, parent, false);
//            holder.thumblayout = (SquareFrameLayout) convertView.findViewById(R.id.video_tailor_frame);
            holder.thumbImage = (ImageView) convertView.findViewById(R.id.video_tailor_img_item);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();

        }
        ViewGroup.LayoutParams lParam = holder.thumbImage.getLayoutParams();
        lParam.width = (screenWidth - ScreenUtil.dip2px( 40)) / 10;
        lParam.height = seekBar.getResSweepLeftHeight();
        holder.thumbImage.setLayoutParams(lParam);
        Bitmap bitmap = mBitmaps.get(position).get();
        if (bitmap != null) {
            holder.thumbImage.setImageBitmap(bitmap);
        }
        return convertView;
    }


    class ViewHolder {
//        private SquareFrameLayout thumblayout;
        private ImageView thumbImage;
    }
}
